//
//  MyFavorites.swift
//  newsApp
//
//  Created by Tirel on 2020/5/13.
//  Copyright © 2020 Doo Psyist. All rights reserved.
//

import Foundation
import SwiftUI

struct MyRecords: View {
    @Environment(\.presentationMode) var presentation
    @State var newsListIteminRecords = Array<NewsListItem>()
    var body: some View {
        NavigationView {
            VStack {
                List {
                    ForEach(newsListIteminRecords) { newsListItemStateOne in
                        NavigationLink(destination: NewsDetail(commentArray: loadComment(id: newsListItemStateOne.id), newsListItem: newsListItemStateOne)) {
                            NewsRow(newsListItem: newsListItemStateOne)
                        }
                    }
                }.onAppear() {
                    self.newsListIteminRecords = []
                    self.loadRecordsData()
                    print(self.newsListIteminRecords)
                }
                HStack {
                    Button(action: {
                        self.presentation.wrappedValue.dismiss()
                    }
) {
                        Text("Back")
                            .bold()
                            .font(.system(size: 30, design: .rounded))
                            .frame(width: 200, height: 100)
                            .foregroundColor(.orange)
                            .shadow(radius: 1)
                    }
                }

            }

        }.navigationBarTitle("")
        .navigationBarHidden(true)
        //.padding(.top, 30)

    }
    func loadRecordsData() {
        for tmp in fetchAll() {
            if tmp.access > 0 {
                for item in networkJson {
                    if Int(tmp.newsid) == item.id {
                        print(tmp.newsid)
                        newsListIteminRecords.append(item)
                    }
                }
            }
        }
    }
}



struct MyRecords_Previews: PreviewProvider {
    static var previews: some View {
        MyRecords()
    }
}
